angular.module('Controller')

.controller('QgyActiveCreateCtrl', ($scope,urls,activeParam,$localStorage,NgTableParams,wfAlert,commonUtil,commonConstant,$rootScope,$uibModal,$http) ->
    $scope.costTypeList=commonConstant.enterFeeTypeList()
    today=new Date(new Date().setHours(0, 0, 0, 0))
    tomorrow=new Date(new Date().setHours(0, 0, 0, 0)).setDate(today.getDate()+1)
    $scope.needVerifyList=commonConstant.needVerifyList()
    initData=()->
        $scope.data={
            organizer:undefined
            name:undefined
            costMoney:undefined
            enterFee:undefined
            needVerify:false
            needFee:false
            address:undefined
            type:undefined
            brief:undefined
            pictures:[]
            startTime:today
            endTime:tomorrow
            enterStart:today
            enterEnd:tomorrow
            params:[]
            basicParamList:[]
            otherParamList:[]
            customParamList:[]
        }
        $scope.activeBasicParams=[]
        $scope.activeOtherParams=[]
        $scope.activeTypeList=[]
        loadActiveType()
        loadActiveParams()
#        console.log(moment(moment().format('YYYY-MM-DD')))
    loadActiveType=()->
        $http.get('/dictionary?dicIndex=3&page=0&size=999')
        .success((resp)->
            if resp.code==0
                $scope.activeTypeList=resp.message.content
                if $scope.activeTypeList.length>0
                    $scope.data.type=$scope.activeTypeList[0].code
        )
    loadActiveParams=()->
        $http.get('/dictionary?dicIndex=16&page=0&size=999')
        .success((resp)->
            if resp.code==0
                list=[]
                _.each resp.message.content,(item)->
                    param=JSON.parse(item.remark)
                    list.push param
                $scope.activeBasicParams=list
                $scope.data.basicParamList=list
        )
        $http.get('/dictionary?dicIndex=18&page=0&size=999')
        .success((resp)->
            if resp.code==0
                list=[]
                _.each resp.message.content,(item)->
                    param=JSON.parse(item.remark)
                    list.push param
                $scope.activeOtherParams=list
        )
    initData()
    _.extend $scope,
        saveData:()->
            params=$scope.data.basicParamList.concat($scope.data.otherParamList)
            params=params.concat($scope.data.customParamList)
            reqData={
                "name": $scope.data.name, #活动名
                "organizer": $scope.data.organizer, #活动组织者
                "brief": $scope.data.brief, #活动简介
                "content": $scope.data.content, #活动内容
                "address": $scope.data.address, #活动地址
                "enterFee": if $scope.data.needFee then $scope.data.enterFee else 0, #报名费
                "needVerify": false, #测试代码 $scope.data.needVerify, #是否需验证码报名
                "needFee": $scope.data.needFee, #是否付费
                "thumb": $scope.data.thumb, #活动封面图地址
                "type": $scope.data.type, #活动类型, 见参数管理，活动类型维护
                "startTime": moment($scope.data.startTime).format('YYYY-MM-DD HH:mm:ss'), #活动开始时间
                "endTime": moment($scope.data.endTime).format('YYYY-MM-DD HH:mm:ss'), #活动结束时间
                "enterStart": moment($scope.data.enterStart).format('YYYY-MM-DD HH:mm:ss'), #活动报名开始时间
                "enterEnd": moment($scope.data.enterEnd).format('YYYY-MM-DD HH:mm:ss'), #活动报名结束时间
                "params": params
            }
            $http.post('/activity',reqData)
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo('创建活动成功')
                    $scope.resetData()
                else
                    wfAlert.toastError(resp.message)
            )
        editThumb:()->
            url=urls.backend
            modal=showUploadModal()
            modal.result.then((modalData)->
                $scope.data.thumb=modalData['saveUrl'] if modalData
            )
        uploadPicture:()->
            url=urls.backend
            modal=showUploadBatchModal({max:10})
            modal.result.then((modalData)->
                return if !modalData || modalData.length==0
                _.each modalData,(item)->
                    $scope.data.pictures.push item['saveUrl']
            )
        removePicture:(item)->
            $scope.data.pictures=_.filter($scope.data.pictures,(pic)->pic!=item)
        editAddress:()->
            showMapModal('edit',{name:$scope.data.address})
        resetData:()->
            initData()
        configParam:()->
            data={
                activeBasicParams:angular.copy $scope.activeBasicParams
                activeOtherParams:angular.copy $scope.activeOtherParams
                otherParamList:$scope.data.otherParamList
                customParamList:$scope.data.customParamList
            }
            showModal('new',data)
    showModal=(type,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyActiveParamConfigCtrl',
            templateUrl: 'templates/active/active-create/active-param-config.html',
            resolve: {
                items:  ()->{type: type, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            if modalData
                $scope.data.otherParamList=modalData.otherParamList
                $scope.data.customParamList=modalData.customParamList
        )
    showMapModal=(type,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'WfMapModalCtrl',
            templateUrl: 'templates/main/map-modal/map-modal.html',
            resolve: {
                items:  ()->{type:type,data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            $scope.data.address=modalData.name if modalData and modalData.name
        )
    showUploadModal=(data)->
        modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'WfUploadModalCtrl',
            templateUrl: 'templates/main/upload-modal/upload-modal.html',
            resolve: {
                items:  ()->{data: data}
            }
        })
    showUploadBatchModal=(data)->
        modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'WfUploadBatchModalCtrl',
            templateUrl: 'templates/main/upload-modal/upload-batch-modal.html',
            resolve: {
                items:  ()->{data: data}
            }
        })
)